package com.itany.service.impl;

import com.itany.entity.ServerType;
import com.itany.entity.ServerTypeExample;
import com.itany.entity.Type;
import com.itany.entity.User;
import com.itany.mapper.ServerTypeMapper;
import com.itany.mapper.TypeMapper;
import com.itany.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper mapper;
    @Autowired
    private ServerTypeMapper serverTypeMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Type findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String parentid, Integer type) {
        List<Type> list = mapper.selectByParams(parentid,type);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()) {
            for (Type t : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",t.getId());
                u.put("name",t.getName());
                u.put("parentName",t.getParentName());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void insertType(String name, String parentid, Integer type) {
        Type type1 = new Type();
        type1.setName(name);
        if(!parentid.isEmpty()){
            type1.setParentid(Integer.parseInt(parentid));
        }
        type1.setType(type);
        mapper.insertSelective(type1);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyType(String name, Integer id) {
        Type type = new Type();
        type.setId(id);
        type.setName(name);
        mapper.updateByPrimaryKeySelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Boolean deleteType(Integer id, Integer type) {
        List<Integer> ids = new ArrayList<>();
        List<Integer> type_ids = new ArrayList<>();
        ids.add(id);
        List<ServerType> serverTypeList = serverTypeMapper.selectByExample(new ServerTypeExample());
        for(ServerType serverType : serverTypeList){
            type_ids.add(serverType.getTypeid());
        }
        List<Type> types = mapper.selectByParams(id.toString(),type);
        if(!types.isEmpty()){
            for(Type type1 : types){
                ids.add(type1.getId());
                List<Type> types1 = mapper.selectByParams(type1.getId().toString(),type);
                if(!types1.isEmpty()){
                    for(Type type2 : types1){
                        ids.add(type2.getId());
                    }
                }
            }
            for(Integer i:ids){
                if(type_ids.contains(i)){
                    return false;
                }
            }
            for (int i = ids.size() - 1; i >= 0; i--) {
                Integer idToDelete = ids.get(i);
                mapper.deleteByPrimaryKey(idToDelete);
            }
            return true;
        }else{
            for(Integer i:ids){
                if(type_ids.contains(i)){
                    return false;
                }
            }
            for(Integer i:ids){
                mapper.deleteByPrimaryKey(i);
            }
            return true;
        }
    }
}
